package com.shelpe.services.sinbad.controller.dto;

import java.util.Date;

import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.BeanUtils;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.shelpe.services.sinbad.model.RptCreative;
import com.shelpe.services.sinbad.model.RptCustomer;
import com.shelpe.services.sinbad.model.RptKeyword;
import com.shelpe.services.sinbad.model.RptKeywordDto;
import com.shelpe.services.sinbad.service.simbaproxy.SimbaProxyBaseDto;
import com.shelpe.services.sinbad.service.simbaproxy.UserClient.GetRtrCustDto;
import com.shelpe.services.sinbad.utils.DateUtil;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = true)
public class EffectDto extends SimbaProxyBaseDto {
	@JsonFormat(shape = Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Shanghai")
	private Date date;
	@JsonFormat(shape = Shape.STRING, pattern = "HH:mm", timezone = "Asia/Shanghai")
	@JsonProperty("minute_date")
	private Date minuteDate;

	@JsonProperty("campaign_id")
	private long campaignID;
	@JsonProperty("adgroup_id")
	private long adgroupID;
	@JsonProperty("keyword_id")
	private long keywordID;
	@JsonProperty("creative_id")
	private long creativeID;
	@JsonProperty("crowd_id")
	private String crowdID;

	@JsonProperty("pay_count")
	private Integer payCount = 0;// 总成交笔数
	@JsonProperty("pay_total")
	private Integer payTotal = 0; // 总成交金额
	@JsonProperty("direct_pay")
	private Integer directPay = 0;// 直接成交金额
	@JsonProperty("indirect_pay")
	private Integer indirectPay = 0;// 间接成交金额
	@JsonProperty("direct_pay_count")
	private Integer directPayCount = 0;// 直接成交笔数
	@JsonProperty("indirect_pay_count")
	private Integer indirectPayCount = 0;// 间接成交笔数
	@JsonProperty("fav_count")
	private Integer favCount = 0;// 总收藏数
	@JsonProperty("fav_shop_count")
	private Integer favShopCount = 0;// 店铺收藏
	@JsonProperty("fav_item_count")
	private Integer favItemCount = 0;// 宝贝收藏
	@JsonProperty("direct_cart_count")
	private Integer directCartTotal = 0;// 直接购物车数
	@JsonProperty("indirect_cart_count")
	private Integer indirectCartTotal = 0;// 间接购物车数
	@JsonProperty("cart_count")
	private Integer cartCount = 0;// 购物车总数

	@JsonProperty("fav_count_rate")
	private Float favCountRate = 0f;
	@JsonProperty("cart_count_rate")
	private Float cartCountRate = 0f;

	private Float avgpos = 0f;// 宝贝排名
	private Float cpc = 0f;// 平均点击花费
	private Float ctr = 0f;// 点击率
	private Integer impressions = 0;// 展现量
	private Float cpm = 0f;// 千次点击
	private Integer click = 0;// 点击量
	private Integer cost = 0;// 总花费
	private Float roi = 0f;// 投入产出比
	private Float cvr = 0f; // 点击转化率

	// 以下是需计算得到
	@JsonProperty("wireless_qscore")
	private Integer wirelessQscore = 0;// 无线质量得分
	private Integer qscore = 0;// 无线质量得分
	@JsonProperty("cart_cost")
	private Float cartCost = 0f; // 加购成本=总花费/总加购数
	@JsonProperty("fav_cart_rate")
	private Float favCartRate = 0f;// 收藏加购率
	@JsonProperty("fav_cart")
	private Float favCart = 0f;// 收藏加购数
	@JsonProperty("fav_cart_throughput")
	private Float favCartThroughput = 0f;// 收藏加购所需流量 点击量/收藏加购数
	@JsonProperty("fav_cost")
	private Float favCost = 0f;// 收藏成本=总花费/总收藏数
	@JsonProperty("pay_count_throughput")
	private Float payCountThroughput = 0f;// 成交所需流量 点击量/成交笔数

	public void from(GetRtrCustDto res) {// TODO:赋值
		this.directPay = (int) NumberUtils.toFloat(res.getDirectTransaction());
		this.indirectPay = (int) NumberUtils.toFloat(res.getIndirectTransaction());
		this.directPayCount = NumberUtils.toInt(res.getDirectTransactionShipping());
		this.indirectPayCount = NumberUtils.toInt(res.getIndirectTransactionShipping());
		this.favItemCount = NumberUtils.toInt(res.getFavItemTotal());
		this.favShopCount = NumberUtils.toInt(res.getFavShopTotal());
		this.favCount = NumberUtils.toInt(res.getFavTotal());
		this.payCount = NumberUtils.toInt(res.getTransactionShippingTotal());
		this.payTotal = (int) NumberUtils.toFloat(res.getTransactionTotal());
		this.directCartTotal = NumberUtils.toInt(res.getDirectCartTotal());
		this.indirectCartTotal = NumberUtils.toInt(res.getIndirectCartTotal());
		this.cartCount = NumberUtils.toInt(res.getCartTotal());
		this.impressions = NumberUtils.toInt(res.getImpression());
		this.click = NumberUtils.toInt(res.getClick());
		this.cost = NumberUtils.toInt(res.getCost());

		this.roi = NumberUtils.toFloat(res.getRoi());
		if (this.click > 0) {
			this.cvr = (float) DateUtil.formatDouble2((double) 100 * this.payCount / this.click);// 直接加百分号
		}
		this.ctr = (float) DateUtil.formatDouble2(NumberUtils.toDouble(res.getCtr()));// 直接加百分号
		this.cpc = (float) DateUtil.formatDouble2(NumberUtils.toDouble(res.getCpc()) / 100);// 单位元
		this.cpm = NumberUtils.toFloat(res.getCpm());
	}

	public void from(RptCustomer rpt) {
		if (rpt != null) {
			BeanUtils.copyProperties(rpt, this);
			if (rpt.getDate() != null) {
				this.date = rpt.getDate();
			}
			this.payCount = (rpt.getDirectPayCount() != null ? rpt.getDirectPayCount() : 0)
					+ (rpt.getIndirectPayCount() != null ? rpt.getIndirectPayCount() : 0);
			this.payTotal = (rpt.getDirectPay() != null ? rpt.getDirectPay() : 0)
					+ (rpt.getIndirectPay() != null ? rpt.getIndirectPay() : 0);
			this.cartCount = rpt.getCartTotal();
			this.favCount = rpt.getFavShopCount() + rpt.getFavItemCount();
			if (rpt.getClick() != null && rpt.getClick() > 0) {
				this.cvr = (float) this.payCount / rpt.getClick();
			}
			if (this.cost != 0) {
				this.roi = (float) this.payTotal / this.cost;
			}
			if (rpt.getImpressions() != null && rpt.getImpressions() > 0) {
				this.ctr = (float) this.click / rpt.getImpressions();
			}
			this.from();
		}
	}

	public void from(RptKeyword rpt) {
		if (rpt != null) {
			BeanUtils.copyProperties(rpt, this);
			if (rpt.getDate() != null) {
				this.date = rpt.getDate();
			}
			this.payCount = (rpt.getDirectPayCount() != null ? rpt.getDirectPayCount() : 0)
					+ (rpt.getIndirectPayCount() != null ? rpt.getIndirectPayCount() : 0);
			this.payTotal = (rpt.getDirectPay() != null ? rpt.getDirectPay() : 0)
					+ (rpt.getIndirectPay() != null ? rpt.getIndirectPay() : 0);
			this.cartCount = rpt.getCartTotal();
			this.favCount = rpt.getFavShopCount() + rpt.getFavItemCount();
			if (rpt.getClick() != null && rpt.getClick() > 0) {
				this.cvr = (float) this.payCount / rpt.getClick();
			}
			if (this.cost != 0) {
				this.roi = (float) this.payTotal / this.cost;
			}
			if (rpt.getImpressions() != null && rpt.getImpressions() > 0) {
				this.ctr = (float) this.click / rpt.getImpressions();
			}
			this.from();
		}
	}

	public void from(RptKeywordDto rpt) {
		if (rpt != null) {
			this.campaignID = rpt.getCampaignId() != null ? rpt.getCampaignId() : 0;
			this.adgroupID = rpt.getAdgroupId() != null ? rpt.getAdgroupId() : 0;
			this.keywordID = rpt.getKeywordId() != null ? rpt.getKeywordId() : 0;
			BeanUtils.copyProperties(rpt, this);
			if (rpt.getDate() != null) {
				this.date = rpt.getDate();
			}
			this.payCount = (rpt.getDirectPayCount() != null ? rpt.getDirectPayCount() : 0)
					+ (rpt.getIndirectPayCount() != null ? rpt.getIndirectPayCount() : 0);
			this.payTotal = (rpt.getDirectPay() != null ? rpt.getDirectPay() : 0)
					+ (rpt.getIndirectPay() != null ? rpt.getIndirectPay() : 0);
			this.cartCount = rpt.getCartTotal();
			this.favCount = rpt.getFavShopCount() + rpt.getFavItemCount();
			if (rpt.getClick() != null && rpt.getClick() > 0) {
				this.cvr = (float) this.payCount / rpt.getClick();
			}
			if (this.cost != 0) {
				this.roi = (float) this.payTotal / this.cost;
			}
			if (rpt.getImpressions() != null && rpt.getImpressions() > 0) {
				this.ctr = (float) this.click / rpt.getImpressions();
			}
			this.from();
		}
	}

	public void from(RptCreative rpt) {
		if (rpt != null) {
			BeanUtils.copyProperties(rpt, this);
			if (rpt.getDate() != null) {
				this.date = rpt.getDate();
			}
			this.payCount = (rpt.getDirectPayCount() != null ? rpt.getDirectPayCount() : 0)
					+ (rpt.getIndirectPayCount() != null ? rpt.getIndirectPayCount() : 0);
			this.payTotal = (rpt.getDirectPay() != null ? rpt.getDirectPay() : 0)
					+ (rpt.getIndirectPay() != null ? rpt.getIndirectPay() : 0);
			this.favCount = rpt.getFavShopCount() + rpt.getFavItemCount();
			if (rpt.getClick() != null && rpt.getClick() > 0) {
				this.cvr = (float) this.payCount / rpt.getClick();
			}
			if (this.cost != 0) {
				this.roi = (float) this.payTotal / this.cost;
			}
			if (rpt.getImpressions() != null && rpt.getImpressions() > 0) {
				this.ctr = (float) this.click / rpt.getImpressions();
			}
			this.from();
		}
	}

	private void from() {
		if (this.payCount == null) {
			this.payCount = 0;
		}
		if (this.payTotal == null) {
			this.payTotal = 0;
		}
		if (this.indirectPay == null) {
			this.indirectPay = 0;
		}
		if (this.directPay == null) {
			this.directPay = 0;
		}
		if (this.indirectPayCount == null) {
			this.indirectPayCount = 0;
		}
		if (this.directPayCount == null) {
			this.directPayCount = 0;
		}
		if (this.favCount == null) {
			this.favCount = 0;
		}
		if (this.favShopCount == null) {
			this.favShopCount = 0;
		}
		if (this.favItemCount == null) {
			this.favItemCount = 0;
		}
		if (this.directCartTotal == null) {
			this.directCartTotal = 0;
		}
		if (this.indirectCartTotal == null) {
			this.indirectCartTotal = 0;
		}
		if (this.cartCount == null) {
			this.cartCount = 0;
		}
		if (this.favCountRate == null) {
			this.favCountRate = 0f;
		}
		if (this.cartCountRate == null) {
			this.cartCountRate = 0f;
		}
		if (this.avgpos == null) {
			this.avgpos = 0f;
		}
		if (this.cpc == null) {
			this.cpc = 0f;
		}
		if (this.ctr == null) {
			this.ctr = 0f;
		}
		if (this.impressions == null) {
			this.impressions = 0;
		}
		if (this.cpm == null) {
			this.cpm = 0f;
		}
		if (this.click == null) {
			this.click = 0;
		}
		if (this.cost == null) {
			this.cost = 0;
		}
		if (this.roi == null) {
			this.roi = 0f;
		}
		if (this.cvr == null) {
			this.cvr = 0f;
		}
		if (this.wirelessQscore == null) {
			this.wirelessQscore = 0;
		}
		if (this.qscore == null) {
			this.qscore = 0;
		}
		if (this.cartCost == null) {
			this.cartCost = 0f;
		}

		if (this.cartCost == null) {
			this.cartCost = 0f;
		}
		if (this.favCartRate == null) {
			this.favCartRate = 0f;
		}
		if (this.favCart == null) {
			this.favCart = 0f;
		}
		if (this.favCartThroughput == null) {
			this.favCartThroughput = 0f;
		}
		if (this.favCost == null) {
			this.favCost = 0f;
		}
		if (this.payCountThroughput == null) {
			this.payCountThroughput = 0f;
		}
	}
}
